<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['course:BearInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['course:BearInfo:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="BearInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程分类" align="center" prop="classTypeName" />
      <el-table-column label="周" align="center" prop="week" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="正式课承担量/单" align="center" prop="formalMax" />
      <el-table-column label="试课承担量/单" align="center" prop="testMax" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['course:BearInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['course:BearInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改承担量信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="课程分类" prop="classTypeId" label-width="125px">
          <el-select v-model="form.classTypeId" placeholder="请选择课程分类" filterable size="large" style="width: 240px">
            <el-option v-for="item in classTypes" value-key="id" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="周" prop="week" label-width="125px">
          <el-select v-model="form.week" placeholder="请选择时间" size="large" style="width: 170px">
            <el-option v-for="item in weeks" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime" label-width="125px">
          <el-time-select v-model="form.startTime" style="width: 240px"  class="mr-4"
            placeholder="请输入开始时间"
                          :picker-options="{start: '00:00',step: '00:15', end: '23:30'}">
          </el-time-select>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime" label-width="125px">
          <el-time-select v-model="form.endTime" style="width: 240px" :min-time="form.startTime" placeholder="请输入结束时间"
                          :picker-options="{start: '00:00',step: '00:15', end: '23:30'}">
          </el-time-select>
        </el-form-item>
        <el-form-item label="正式课承担量/单" prop="formalMax" label-width="125px">
          <el-input-number size="large" v-model="form.formalMax" placeholder="请输入正式课承担量">
            <template #suffix>
              <span>单</span>
            </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="试课承担量/单" prop="testMax" label-width="125px">
          <el-input-number size="large" v-model="form.testMax" placeholder="请输入试课承担量">
            <template #suffix>
              <span>单</span>
            </template>
          </el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBearInfo, getBearInfo, delBearInfo, addBearInfo, updateBearInfo } from "@/api/course/BearInfo";
import { listType } from "@/api/business/kctype"

export default {
  name: "BearInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承担量信息表格数据
      BearInfoList: [],
      // 课程分类数据
      classTypes: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classTypeId: null,
        classTypeName: null,
        startTime: null,
        endTime: null,
        formalMax: null,
        testMax: null,
      },
      // 表单参数
      form: {
        startTime: '',
        endTime: '',
      },
      // 表单校验
      rules: {
        classTypeId: [{ required: true, message: "请选择课程分类", trigger: "change" }],
        week: [{ required: true, message: "请选择周", trigger: "change" }],
        startTime: [{ required: true, message: "请选择开始时间", trigger: "change" }],
        endTime: [{ required: true, message: "请选择结束时间", trigger: "change" }],
        formalMax: [{ required: true, message: "请输入正式课承担量", trigger: "blur" }],
        testMax: [{ required: true, message: "请输入试课承担量", trigger: "blur" }]
      },
      weeks: [
        { value: '星期一', label: '星期一' },
        { value: '星期二', label: '星期二' },
        { value: '星期三', label: '星期三' },
        { value: '星期四', label: '星期四' },
        { value: '星期五', label: '星期五' },
        { value: '星期六', label: '星期六' },
        { value: '星期日', label: '星期日' },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询承担量信息列表 */
    getList() {
      this.loading = true;
      listBearInfo(this.queryParams).then(response => {
        this.BearInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listType(this.queryParams1).then(response => {
        this.classTypes = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classTypeId: null,
        classTypeName: null,
        startTime: '',
        endTime: '',
        formalMax: null,
        testMax: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加承担量信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBearInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改承担量信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBearInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBearInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除承担量信息编号为"' + ids + '"的数据项？').then(function () {
        return delBearInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/BearInfo/export', {
        ...this.queryParams
      }, `BearInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
